package GameLogic
{
	import BasicEngine.Dispatcher;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	import SoundManager.SoundFactory;
	
	public class SettingButton extends Sprite
	{
		public  var Frame:Bitmap;
		public var SettingPanel:SetPanel;

		private var pics:Vector.<BitmapData>;
		private var index:int = 0;
		private var privateTimer:int = 0;
		private var shape:Shape;
		private var isAnimation:Boolean = false;
		private var showSetPanel:Boolean = false;
		
		public function SettingButton()
		{
		    pics = new Vector.<BitmapData>();
			
			var p:Array = [4043, 4044, 4045, 4046, 4047,4048];
			for each(var item:int in p)
			{
				pics.push(BmpDataPool.LuaeditPool[item]);
			}
			
			Frame = new Bitmap(pics[index]);
			shape = new Shape();
			shape.graphics.beginFill(0xffffff, 1);
			shape.graphics.drawCircle(29, 27, 28);
			addChild(shape);
			addChild(Frame);
			SettingPanel =  new SetPanel();
			Game.MenuCanvas.addChild(SettingPanel);
			SettingPanel.x = 760;
			SettingPanel.y = 90;
			SettingPanel.visible = false;
			this.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				if (Dispatcher.Clock.SystemTime - privateTimer > 200)
				{
				    if (index > pics.length - 1)
				       index = 0;
				    Frame.bitmapData = pics[index];
					if (isAnimation)
				    {
				       index++;
				    }
					privateTimer = Dispatcher.Clock.SystemTime;
					if (Dispatcher.Clock.SystemTime % 10000 == 0)
					{
						isAnimation = true;
					}
				}
			});
			this.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{
				Close();
				
			});
			this.addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent):void
			{
				Mouse.cursor = MouseCursor.BUTTON;
				isAnimation = true;
			});
			this.addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent):void
			{
				Mouse.cursor = MouseCursor.AUTO;
				isAnimation = false;
			});
		}
		
		public function Close():void
		{
			SoundFactory.Sounds["button1"].play();
			if (!SettingPanel.visible)
			{
				SettingPanel.visible = true;
				SettingPanel.addEventListener(Event.ENTER_FRAME, T);
			}
			else
			{
				SettingPanel.addEventListener(Event.ENTER_FRAME, F);
			}
		}
		
		private function T(e:Event):void
		{
			SettingPanel.x -= 75;
			SettingPanel.alpha += 0.3;			
			if (SettingPanel.x <= 250)
			{
				SettingPanel.alpha = 3;
				SettingPanel.removeEventListener(Event.ENTER_FRAME, T);
			}
		}
		
		private function F(e:Event):void
		{
			SettingPanel.x += 65;
			SettingPanel.alpha -= 0.35;
			if (SettingPanel.x >= 760)
			{
				SettingPanel.removeEventListener(Event.ENTER_FRAME, F);
				SettingPanel.visible = false;
			}
		}
	}
}